﻿using Models.ScanNetWorkMac;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UtilsORM;

namespace GateWayCloodService.CloudCenter
{
    public class ServiceCloudNetWork
    {
        /// <summary>
        /// 微服务器网络IP更新
        /// </summary>
        /// <param name="netWorkMacInfos"></param>
        public void pushNetWorkMacData(List<NetWorkMacInfo> netWorkMacInfos)
        {
            try
            {
                foreach (NetWorkMacInfo netWorkMacInfo in netWorkMacInfos)
                {
                    if (netWorkMacInfo.mac == null) continue;
                    Debug.WriteLine("\nRabbitMQ  miniServer_NetWorkMacList  更新mac！");
                    string sql = "select d_guid from iot_device   where" +
                        " d_mac like '%" + netWorkMacInfo.mac.Replace(":", "-") + "%' or d_mac like '%" + netWorkMacInfo.mac.Replace("-", ":") + "%'" +
                        " or d_mac in ('" + netWorkMacInfo.mac.Replace(":", "-") + "','" + netWorkMacInfo.mac.Replace("-", ":") + "','" + netWorkMacInfo.mac.Replace(":", "-").Replace(",", "','") + "','" + netWorkMacInfo.mac.Replace("-", ":").Replace(",", "','") + "') ";
                    DataTable dt = MySqlYihongIot.GetDataTable(sql);
                    foreach (DataRow row in dt.Rows)
                    {
                        sql = "update iot_device set d_local_ip='" + netWorkMacInfo.localIp + "',d_mod_time='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where d_guid='" + row["d_guid"] + "'";
                        MySqlYihongIot.ExecuteCommand(sql);

                        sql = "update iot_device_jidianqi set dj_local_ip='" + netWorkMacInfo.localIp + "',dj_mod_time='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where  dj_d_guid='" + row["d_guid"] + "'";
                        MySqlYihongIot.ExecuteCommand(sql);

                        sql = "update iot_device_gateway set gw_local_ip='" + netWorkMacInfo.localIp + "',gw_mod_time='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "' where  gw_d_guid='" + row["d_guid"] + "'";
                        MySqlYihongIot.ExecuteCommand(sql);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
}
